package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.activity.LoginActivity;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.LoginItem;

public class LoginThread extends BasicThread {

	public interface LoginThreadCallback {
		public void OnLoginCompleted(boolean isNetworkConntected, LoginItem loginItem, String message);
		public void OnLoginError(ErrorItem errorItem);
	}

	private LoginThreadCallback callback;
	private String userId;
	
	//private String url;
	//private ArrayList<NameValuePair> post;
	
	public LoginThread(Context context, LoginThreadCallback callback, String userId) {
		super(userId);
		this.context = context;
		this.callback = callback;
		this.userId = userId;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}
	
	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		map.put(Const.USERID, userId);
		String param = parameterReturn(Const.REQ001, (HashMap<String, String>) map);
		Log.i("userId", userId);
		Log.i("param", param);
		
		serviceBroker(param, context);
		//serviceTestBroker(param);
		
	}

	@Override
	public void settingObject() {
		try {
			//테스트 시작
			/*JSONParser jParser = new JSONParser();
			
			String url = "http://10.110.128.40:8086/index.do"; 
			ArrayList<NameValuePair> post = ParameterUtil.getLoginPostParams(userId);
			JSONObject json = jParser.getJSONFromUrl(url, post);
			Log.i("url","url = " + url + post);
			Log.i("json","json = " + json);*/
			
			//테스트 끝
			
			//진짜
			json = new JSONObject(returnJson);
			if(json == null) {
				callback.OnLoginError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			if(result == 0){
				String data = json.getString(Const.DATA);
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				
				callback.OnLoginError(errorItem);
			}else{
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				JSONObject login = arrayLogin.getJSONObject(0);
				LoginItem loginItem = new LoginItem();
				loginItem.setUserSn(appStatus.utilm.getStringJSONObject(login, LoginItem.USERSN));
				loginItem.setUserNm(appStatus.utilm.getStringJSONObject(login, LoginItem.USERNM));
				loginItem.setUserEngNm(appStatus.utilm.getStringJSONObject(login, LoginItem.USERENGNM));
				loginItem.setUserAcnt(appStatus.utilm.getStringJSONObject(login, LoginItem.USERACNT));
				loginItem.setSflowId(appStatus.utilm.getStringJSONObject(login, LoginItem.SFLOWID));
				loginItem.setAuthorPolicySn(appStatus.utilm.getStringJSONObject(login, LoginItem.AUTHORPOLICYSN));
				loginItem.setUpPstinstInsttCode(appStatus.utilm.getStringJSONObject(login, LoginItem.UPPSTINSTINSTTCODE));
				loginItem.setPstinstInsttCode(appStatus.utilm.getStringJSONObject(login, LoginItem.PSTINSTINSTTCODE));
				loginItem.setUserSeCode(appStatus.utilm.getStringJSONObject(login, LoginItem.USERSECODE));
				loginItem.setMbtlnum(appStatus.utilm.getStringJSONObject(login, LoginItem.MBTLNUM));
				loginItem.setInsttLevel(appStatus.utilm.getStringJSONObject(login, LoginItem.INSTTLEVEL));
				loginItem.setQesInsttLevel(appStatus.utilm.getStringJSONObject(login, LoginItem.QESINSTTLEVEL));
				loginItem.setEntrpsUnitySn(appStatus.utilm.getStringJSONObject(login, LoginItem.ENTRPSUNITYSN));
				loginItem.setEntrpsUnityNm(appStatus.utilm.getStringJSONObject(login, LoginItem.ENTRPSUNITYNM));
				loginItem.setOffCd(appStatus.utilm.getStringJSONObject(login, LoginItem.OFFCD));
				loginItem.setTelno(appStatus.utilm.getStringJSONObject(login, LoginItem.TELNO));
				loginItem.setAllInsttNm(appStatus.utilm.getStringJSONObject(login, LoginItem.ALLINSTTNM));
				loginItem.setVersion(appStatus.utilm.getStringJSONObject(login, LoginItem.VERSION));
				//버젼확인
				if(!loginItem.getVersion().equals(LoginActivity.VERSION)){
					ErrorItem errorItem = new ErrorItem();
					errorItem.setrDesc("최신버젼("+loginItem.getVersion()+")으로 업데이트 하세요.");
					callback.OnLoginError(errorItem);
					return;
				}
				appStatus.setLoginItem(loginItem);
				callback.OnLoginCompleted(true, loginItem, mResult);
			}
		}catch (JSONException e) {
			e.printStackTrace();
		}
	}
}